package tt.dz.util;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * MD5加密工具类
 * 
 * @author 杜贵科
 *
 */
public class DzDigester {

	private static final String DEFAULT_CHARSET = "UTF-8";
    // 算法名称  
    public static final String KEY_ALGORITHM = "DES";  
    // 算法名称/加密模式/填充方式  
    public static final String CIPHER_ALGORITHM_ECB = "DES";  


	/**
	 * 
	 * @param res
	 *            原始密码
	 * @param rander
	 *            随机数
	 * @return
	 * @throws Exception
	 */
	public final static String md5ForUser(String res, String rander) {
		try {
			return DigestUtils.md5Hex(DigestUtils.md5Hex(res.getBytes(DEFAULT_CHARSET)) + rander);
		} catch (UnsupportedEncodingException e) {
			  return null ;
		}
	}

	public final static String sha1ForOper(String res) throws Exception {
		try {
			return DigestUtils.sha1Hex(res.getBytes(DEFAULT_CHARSET));
		} catch (Exception e) {
			throw e;
		}
	}


    /** 
     * 生成密钥 
     *  
     * @return 
     * @throws Exception 
     */  
    private static String generateKey() throws Exception {  
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);  
        keyGenerator.init(56); //des 必须是56, 此初始方法不必须调用  
        SecretKey secretKey = keyGenerator.generateKey();  
        return Base64.encodeBase64String(secretKey.getEncoded());  
    }  
  
    /** 
     * 还原密钥 
     *  
     * @param key 
     * @return 
     * @throws Exception  
     */  
    private static Key toKey(String key) throws Exception {  
        DESKeySpec des = new DESKeySpec(Base64.decodeBase64(key));  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);  
        SecretKey secretKey = keyFactory.generateSecret(des);  
        return secretKey;  
    }  
      
    /** 
     * 加密  
     * @param data 原文 
     * @param key 
     * @return 密文 
     * @throws Exception 
     */  
    public static String desEncrypt(String data, String key) throws Exception
    {
        Key k = toKey(key);  
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
        cipher.init(Cipher.ENCRYPT_MODE, k);  
        return Base64.encodeBase64String(cipher.doFinal(data.getBytes(DEFAULT_CHARSET)));  
    }  
    /** 
     * 解密 
     * @param data 密文 
     * @param key 
     * @return 明文、原文 
     * @throws Exception 
     */   
    public static String desDecrypt(String data, String key) throws Exception {  
        Key k = toKey(key);  
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);  
        cipher.init(Cipher.DECRYPT_MODE, k);  
        return new String(cipher.doFinal(Base64.decodeBase64(data)));  
    } 
    
    public static void main(String[] args) throws Exception {
		String res = "hello Word订单";
    	String key  = generateKey();
		System.out.println("key:"+key);
		String en = desEncrypt(res,key);
		System.out.println("密文："+en);
		String de = desDecrypt(en,key);
		System.out.println("解密："+de);
		System.out.println("解密是否成功："+de.equals(res));
	}
}